﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Left : System.Web.UI.Page
{
    protected DataTable dt;
    protected Hashtable ht;
    private TreeView treeContent;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["Users"] == null)
            {
                Response.Write("<script>window.parent.location.href='Login.aspx'</script>");
            }
            else
            {
                string[] user = (string[])Session["Users"];
                ht = (Hashtable)Session["Functions"];
                CreateDs(user[0]);
                Bind_head();
            }
        }

    }
    #region 创建meau
    private void CreateDs(string usercode)
    {
        DataSet ds = new BLL.USERS().GetModuleList(usercode, "UserName");//获取菜单
        dt = ds.Tables[0];
        dt.TableName = "model";
    }
    private void Bind_head()
    {
        AjaxControlToolkit.AccordionPane pane;
        Label lblHeader;
        dt.DefaultView.RowFilter = "FUNC_FATHERCODE=0";
        dt.DefaultView.Sort = " [FUNC_CODE] ASC";
        DataTable dt0 = dt.DefaultView.ToTable();
        for (int i = 0; i < dt0.Rows.Count; i++)
        {
            pane = new AjaxControlToolkit.AccordionPane();
            //第一级菜单 
            lblHeader = new Label();
            lblHeader.Font.Bold = true;
            lblHeader.Text = " <span onclick= 'ClearBgcolor(this)'> "+ dt0.Rows[i]["FUNC_NAME"].ToString() +"</span>" ;
            if (ht.ContainsValue(dt0.Rows[i]["FUNC_CODE"].ToString().Trim()))
            {
                pane.HeaderContainer.Controls.Add(lblHeader);
                treeContent = new TreeView();
                BindContent(null, Convert.ToInt32(dt0.Rows[i]["FUNC_CODE"]));
                pane.ContentContainer.Controls.Add(treeContent);
                Accordion1.Panes.Add(pane);
            }
        }
        
    }
    private void BindContent(TreeNode currentNode, int FormId)
    {
        TreeNode node;
        dt.DefaultView.RowFilter = "FUNC_FATHERCODE =" + FormId;
        dt.DefaultView.Sort = " [FUNC_CODE] ASC";
        DataTable dt1 = dt.DefaultView.ToTable();
        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            node = new TreeNode();
            node.Text = " <span   onclick= 'SetTrBgcolor(this)'> " + dt1.Rows[i]["FUNC_NAME"].ToString() + " </span> ";//Node.Text= " <span   onclick= '*** '> "+   text+ " </span> "; 
            
            if (ht.ContainsValue(dt1.Rows[i]["FUNC_CODE"].ToString().Trim()))
            {
                if (String.IsNullOrEmpty(dt1.Rows[i]["FUNC_URL"].ToString()))
                {
                    node.SelectAction = TreeNodeSelectAction.None;
                }
                else
                {
                    node.NavigateUrl = BLL.Common.ApplicationPath + dt1.Rows[i]["FUNC_URL"].ToString() + "?FCode=" + dt1.Rows[i]["FUNC_CODE"].ToString();
                    //node.ImageUrl = "_images/a.gif";
                    node.Target = "MainFrame";
                    
                }

                if (currentNode != null)
                {
                    currentNode.ChildNodes.Add(node);
                }
                else
                {
                    treeContent.Nodes.Add(node);
                    treeContent.ShowExpandCollapse = false;
                }
                BindContent(node, Convert.ToInt32(dt1.Rows[i]["FUNC_CODE"]));
            }
        }

    }
    #endregion
}
